package mqpaydemo.paydemo.pay.portal.rabbitMq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.extern.slf4j.Slf4j;
import mqpaydemo.paydemo.pay.api.MoneyService;
import mqpaydemo.paydemo.pay.api.UserService;
import mqpaydemo.paydemo.pay.orm.entity.Money;
import mqpaydemo.paydemo.pay.orm.entity.User;
import mqpaydemo.paydemo.pay.request.OrdersRequest;
import mqpaydemo.paydemo.pay.response.Result;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Description:
 * @Author: liujinsheng
 * @Date: 2021/9/15.
 */
@Component
@Slf4j
public class PayRecivcer {
    @Resource
    UserService userService;
    @Resource
    MoneyService moneyService;
    /**
     * 放在方法上
     * @param msg
     */
    @RabbitHandler
    @RabbitListener(queues = "payQuequ")
    public void payRecivcer(Map<String, Object> msg) {
        try {
            //TODO：接收String
            log.info("接收String消息： {} ", msg);
            //map转化为对象
            OrdersRequest ordersRequest= JSON.parseObject(JSON.toJSONString(msg),OrdersRequest.class);
            log.info("最终转化的数据"+ordersRequest);
            //找出用户
            if(ordersRequest!=null){
                Long  id=ordersRequest.getUserId();
               //查找用户现在的余额
                QueryWrapper queryWrapper=new QueryWrapper();
                queryWrapper.eq("uid",id);
                Money money=moneyService.getOne(queryWrapper);
                BigDecimal uprice=null;
                if(money!=null){
                   uprice=  money.getAmount();
                }
                Money upmMoney=new Money();
                upmMoney.setId(money.getId());
                upmMoney.setUid(id);
               BigDecimal price= ordersRequest.getPrice();
               //余额减去价格
                log.info("当前用户的金额{}",uprice);
                if(uprice!=null){
                 BigDecimal finalPrice=   uprice.subtract(price);
                 log.info("最终的余额{}",finalPrice);
                    upmMoney.setAmount(finalPrice);
                }




                //去扣钱
                try {
                    System.out.println("要修改的对象"+upmMoney);
                    Boolean bln = moneyService.updateById(upmMoney);
                    if(bln){
                        //给另一个用户加钱
                        Money moneyTo=null;
                        //查出要转入的用户
                        QueryWrapper queryWrapperTo=new QueryWrapper();
                        queryWrapperTo.eq("id",16L);
                        moneyTo=moneyService.getOne(queryWrapperTo);
                        if(moneyTo==null){
                            log.error("收款账户不存在");
                        }
                        BigDecimal priceTo=    moneyTo.getAmount();
                        //加上价格
                        BigDecimal toPrice=   priceTo.add(price);
                        Money money1=new Money();
                        money1.setId(16L);
                        money1.setUid(4L);
                        money1.setAmount(toPrice);
                        System.out.println("要转入的金额");
                        try {
                            Boolean blnTo=      moneyService.updateById(money1);
                            if(blnTo){
                                log.info("转入收账方成功");
                            }else{
                                log.error("转入收账方失败");
                            }

                        }catch (Exception exception){
                            log.error("转入收账方失败");
                        }
                        log.info("扣钱成功");
                    }else {
                        log.error("扣钱失败");
                    }
                }catch (Exception e){
                    log.error("数据库连接异常");
                }

            }


        } catch (Exception e) {
            log.error("监听消费消息 发生异常： ", e.getMessage());
        }
    }
}
